# @Version        : 1.0
# @Update Time    : 2024/12/11 22:21
# @File           : dept_controller.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息
from fastapi import APIRouter, Depends
from fastapi_utils.cbv import cbv
from sqlalchemy.ext.asyncio import AsyncSession

from authx.dependencies.auth import RequireLogin
from authx.dependencies.permission import PermissionChecker
from src import schemas
from src.core.base_controller import BaseController
from src.core.dependencies import IdList
from src.core.response.response_schema import (
    ResponseGenericSchema,
    ResponseSchema,
    PageResponse,
)
from src.db.db_database import alchemy
from src.models import SysDept
from src.schemas.dept_schema import DeptParams
from src.services.dept_service import DeptService
from src.utils.toolsutils import ToolsUtils

router = APIRouter()


@cbv(router)
class DeptController(BaseController):
    db: AsyncSession = Depends(alchemy.provide_session())
    dept_service: DeptService = Depends(alchemy.provide_service(DeptService))

    @router.get(
        "/list",
        summary="部门列表",
        response_model=PageResponse[schemas.DeptTree],
        dependencies=[Depends(RequireLogin())],
    )
    async def get_dept_list(
        self,
        param: DeptParams = Depends(),
    ):
        where = await param.get_where_and_limit_offset(SysDept)
        res, count = await self.dept_service.list_and_count(*where)
        res = ToolsUtils.build_tree(
            [i.to_dict() for i in res], parent_id=None, parent_name="parent_id"
        )
        return self.paginated_response(
            data=res, total=count, current=param.page, size=param.limit
        )

    @router.get(
        "/tree",
        summary="部门树",
        response_model=ResponseGenericSchema[list[schemas.DeptTree]],
        dependencies=[Depends(RequireLogin())],
    )
    async def get_dept_tree(self):
        res = await self.dept_service.list()
        res = ToolsUtils.build_tree(
            [i.to_dict() for i in res], parent_id=None, parent_name="parent_id"
        )
        return self.success(data=res)

    @router.post(
        "/add",
        summary="创建部门",
        description="创建部门",
        dependencies=[Depends(PermissionChecker("system:dept:add"))],
        response_model=ResponseSchema,
    )
    async def add_dept(
        self,
        model: schemas.DeptCreate,
    ):
        await self.dept_service.create_dept(model)
        return self.success(message="创建成功")

    @router.put(
        "/update",
        summary="更新部门",
        description="更新部门",
        dependencies=[Depends(PermissionChecker("system:dept:update"))],
        response_model=ResponseSchema,
    )
    async def update_dept(
        self,
        model: schemas.DeptUpdate,
    ):
        await self.dept_service.update_dept(model)
        return self.success(message="更新成功")

    @router.delete(
        "/delete",
        summary="删除部门",
        description="删除部门",
        dependencies=[Depends(PermissionChecker("system:dept:delete"))],
        response_model=ResponseSchema,
    )
    async def delete_dept(
        self,
        ids: IdList = Depends(),
    ):
        await self.dept_service.delete_many(ids.ids, auto_commit=True)
        return self.success(message="删除成功")
